(function () {
  'use strict';

  angular
    .module('vboxApp')
    .controller('dashboardSummaryCtrl', dashboardSummaryCtrl)
    .controller('importCtrl', importCtrl)
    .controller('appealCtrl', appealCtrl)
    .controller('dealCtrl', dealCtrl);

  /** @ngInject */
  function dashboardSummaryCtrl($scope, $log, dashboardSummaryRepo, $uibModal, dialogs, $window) {

    $scope.currentPage = 1;

    //获取所有文件
    function getFileList(page) {
      dashboardSummaryRepo.getFiles(page, function (data) {
        $scope.fileData = data.returnValue.content;
        $scope.totalElements = data.returnValue.totalElements;
      });
      dashboardSummaryRepo.getSummaryFiles(function (data) {
        $scope.summaryFileData = data.returnValue;
      });
    }

    getFileList(1);

    $scope.pageChanged = function () {
      getFileList($scope.currentPage);
    };

    //文件上传modal
    $scope.import = function () {
      var modal = $uibModal.open({
        animation: false,
        size: 'md',
        backdrop: 'static',
        templateUrl: 'app/modules/dashboard/summary/importModal.html',
        controller: 'importCtrl',
        resolve: {
          vm: function () {
            return {};
          }
        }
      });
      modal.result.then(function () {
        $scope.currentPage = 1;
        getFileList(1);
      });
    };

    //文件申诉
    $scope.appeal = function (fileName, fileStatus, appealContext, answerContext, id) {

      var modal = $uibModal.open({
        animation: false,
        size: 'md',
        backdrop: 'static',
        templateUrl: 'app/modules/dashboard/summary/appealModal.html',
        controller: 'appealCtrl',
        resolve: {
          vm: function () {
            return {
              fileName: fileName,
              fileStatus: fileStatus,
              appealContext: appealContext,
              answerContext: answerContext
            };
          }
        }
      });
      modal.result.then(function (data) {
        //$log.info(data);
        dashboardSummaryRepo.appealFile(id, data, function (res) {
          $log.info(res);
          if (res.success) {
            dialogs.notify("通知", "文件&nbsp;" + fileName + "&nbsp;申诉成功！", {size: 'sm', windowClass: 'dialogs-inverse'});
            getFileList($scope.currentPage);
          }
        });
      });
    };

    //处理申诉
    $scope.deal = function (fileName, fileStatus, appealContext, answerContext, id) {
      var modal = $uibModal.open({
        animation: false,
        size: 'md',
        backdrop: 'static',
        templateUrl: 'app/modules/dashboard/summary/dealModal.html',
        controller: 'dealCtrl',
        resolve: {
          vm: function () {
            return {
              fileName: fileName,
              fileStatus: fileStatus,
              appealContext: appealContext,
              answerContext: answerContext
            };
          }
        }
      });
      modal.result.then(function (data) {
        //$log.info(data);
        dashboardSummaryRepo.dealFile(id, data, function (res) {
          $log.info(res);
          if (res.success) {
            dialogs.notify("通知", "文件&nbsp;" + fileName + "&nbsp;申诉处理成功！", {size: 'sm', windowClass: 'dialogs-inverse'});
            getFileList($scope.currentPage);
          }
        });
      });
    };

    //文件下载
    $scope.download = function (fileTag, fileName) {
      dashboardSummaryRepo.downloadFile(function (url) {
        $window.location.href = url.returnValue + '/' + fileTag + '/' + fileName;
      });

    };

    //文件删除
    $scope.delete = function (fileName, id) {
      dialogs.confirm("删除文件", "确认删除文件&nbsp;" + fileName + "&nbsp;吗？", {
        size: 'sm',
        windowClass: 'dialogs-inverse'
      }).result.then(function () {
        dashboardSummaryRepo.deleteFile(id, function (data) {
          $log.info(data);
          if (data.success) {
            dialogs.notify("通知", "文件&nbsp;" + fileName + "&nbsp;已成功删除！", {size: 'sm', windowClass: 'dialogs-inverse'});
            $scope.currentPage = 1;
            getFileList(1);
          }
        });
      });
    };

  }

  /** @ngInject */
  function importCtrl($scope, $log, $uibModalInstance, Upload, CommonVar) {

    $scope.showFiles = false;
    $scope.showSuccessInfo = false;

    //上传文件
    $scope.onSelect = function (files) {

      if (files) {
        $scope.showFiles = true;
        $scope.files = files;

        $scope.result = [];

        for (var i = 0; i < $scope.files.length; i++) {

          (function (e) {
            Upload.upload({
              url: CommonVar.url + 'file/upload?tag=' + $scope.tag,
              file: $scope.files[e]
            }).then(function (resp) {
              console.log('Success ' + resp.config.file.name + ' uploaded. Response: ' + resp.data.returnValue);
              $scope.files[e].success = resp.data.success;
              if (resp.data.success) {
                $scope.result.push('a');
                if ($scope.result.length === $scope.files.length) {
                  $scope.showSuccessInfo = true;
                }
              } else {
                $scope.files[e].success = "上传失败," + resp.data.returnValue;
              }

            }, function (resp) {
              console.log('Error status: ' + resp.status);
            }, function (evt) {
              var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
              console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);

              $scope.files[e].progress = progressPercentage;

            });
          })(i)

        }
      }

    };

    $scope.save = function () {
      $uibModalInstance.close();
    };

    // $scope.close = function () {
    //   $uibModalInstance.dismiss('cancel');
    // };

  }

  /** @ngInject */
  function appealCtrl($scope, $log, vm, $uibModalInstance) {

    $scope.fileName = vm.fileName;
    $scope.fileStatus = vm.fileStatus;
    $scope.appealContext = vm.appealContext;
    $scope.answerContext = vm.answerContext;


    $scope.save = function () {
      //$log.info($scope.appealContext);
      $uibModalInstance.close($scope.appealContext);
    };

    $scope.close = function () {
      $uibModalInstance.dismiss('cancel');
    };

  }

  /** @ngInject */
  function dealCtrl($scope, $log, vm, $uibModalInstance) {

    $scope.fileName = vm.fileName;
    $scope.fileStatus = vm.fileStatus;
    $scope.appealContext = vm.appealContext;
    $scope.answerContext = vm.answerContext;

    $scope.save = function () {
      //$log.info($scope.answerContext);
      $uibModalInstance.close($scope.answerContext);
    };

    $scope.close = function () {
      $uibModalInstance.dismiss('cancel');
    };

  }

})();
